// mainPackage/pages/commentList/commentList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [{
        id: 4,
        text: '全部',
        page: 1,
        isFinish: false,
        list: []
      },
      {
        id: 0,
        text: '好评',
        page: 1,
        isFinish: false,
        list: []
      },
      {
        id: 1,
        text: '中评',
        page: 1,
        isFinish: false,
        list: []
      },
      {
        id: 2,
        text: '差评',
        page: 1,
        isFinish: false,
        list: []
      },
      {
        id: 3,
        text: '有图',
        page: 1,
        isFinish: false,
        list: []
      },
    ],
    selectIndex: 0,
    productId: '', //商品id
    pageSize: 10
  },

  // 选择tab栏   几星
  selectTab(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    this.setData({
      selectIndex: index
    })
    this.getList(1)
  },



  getList(flag) {
    var url = app.globalData.url + 'ScComment/getProductByProductId',
      that = this,
      navList = this.data.navList,
      selectIndex = this.data.selectIndex;
    if (flag == 1 && navList[selectIndex].list.length != 0) {
      return
    }
    app.wxRequest(url, {
        productId: that.data.productId,
        // productId: 14,
        commentType: navList[selectIndex].id,
        pageSize: that.data.pageSize,
        pageNum: navList[selectIndex].page
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          res.list.forEach(item => {
            if (!item.userImg) {
              item.userImg = '/img/avatar.png'
            }
          })

          navList[selectIndex].list.push(...res.list)
          navList[selectIndex].isFinish = res.list.length < that.data.pageSize
          navList[selectIndex].page++
          console.log(navList);
          console.log(res);
          that.setData({
            navList
          })
          wx.stopPullDownRefresh()
          wx.hideLoading()
        }
      },
      err => {
        console.log(err);
      })
  },

  // 图片加载失败
  errorHandle(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index,
      selectIndex = this.data.selectIndex,
      navList = this.data.navList;
    navList[selectIndex].list[index].userImg = '/img/avatar.png'
    this.setData({
      navList
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      productId: options.id
    })
    this.getList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    var navList = this.data.navList,
      selectIndex = this.data.selectIndex;

    if (navList[selectIndex].isFinish) {
      wx.showToast({
        title: '暂无更多评论',
        icon: 'none'
      })
    } else {
      this.getList(2)
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})